home *** CD-ROM | disk | FTP | other *** search
/ Linux Cubed Series 2: Applications / Linux Cubed Series 2 - Applications.iso / editors / emacs / xemacs / xemacs-1.006 / xemacs-1 / lib / xemacs-19.13 / lisp / games / blackbox.el next >
Encoding:
Text File  |  1995-03-25  |  14.5 KB  |  438 lines

  1. ;;; blackbox.el --- blackbox game in Emacs Lisp
  2.  
  3. ;; Copyright (C) 1985, 1986, 1987, 1992 Free Software Foundation, Inc.
  4.  
  5. ;; Author: F. Thomas May <uw-nsr!uw-warp!tom@beaver.cs.washington.edu>
  6. ;; Adapted-By: ESR
  7. ;; Keywords: games
  8.  
  9. ;; This file is part of XEmacs.
  10.  
  11. ;; XEmacs is free software; you can redistribute it and/or modify it
  12. ;; under the terms of the GNU General Public License as published by
  13. ;; the Free Software Foundation; either version 2, or (at your option)
  14. ;; any later version.
  15.  
  16. ;; XEmacs is distributed in the hope that it will be useful, but
  17. ;; WITHOUT ANY WARRANTY; without even the implied warranty of
  18. ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
  19. ;; General Public License for more details.
  20.  
  21. ;; You should have received a copy of the GNU General Public License
  22. ;; along with XEmacs; see the file COPYING.  If not, write to the Free
  23. ;; Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.
  24.  
  25. ;;; Synched up with: FSF 19.28.
  26.  
  27. ;;; Commentary:
  28.  
  29. ; by F. Thomas May <uw-nsr!uw-warp!tom@beaver.cs.washington.edu>
  30. ; doc comment by Root Boy Jim <rbj@dsys.icst.nbs.gov>, 27 Apr 89
  31. ; interface improvements by ESR, Dec 5 1991.
  32.  
  33. ; The object of the game is to find four hidden balls by shooting rays
  34. ; into the black box.  There are four possibilities: 1) the ray will
  35. ; pass thru the box undisturbed, 2) it will hit a ball and be absorbed,
  36. ; 3) it will be deflected and exit the box, or 4) be deflected immediately,
  37. ; not even being allowed entry into the box.
  38. ; The strange part is the method of deflection.  It seems that rays will
  39. ; not pass next to a ball, and change direction at right angles to avoid it.
  40. ;                      R   3   
  41. ;          1 - - - - - - - - 1 
  42. ;            - - - - - - - -   
  43. ;            - O - - - - - - 3 
  44. ;          2 - - - - O - O -   
  45. ;          4 - - - - - - - - 
  46. ;          5 - - - - - - - - 5 
  47. ;            - - - - - - - - R 
  48. ;          H - - - - - - - O   
  49. ;            2   H 4       H   
  50. ; Rays which enter and exit are numbered.  You can see that rays 1 & 5 pass
  51. ; thru the box undisturbed. Ray 2 is deflected by the northwesternmost
  52. ; ball.  Likewise rays 3 and 4. Rays which hit balls and are absorbed are
  53. ; marked with H.  The bottom of the left and the right of the bottom hit
  54. ; the southeastern ball directly.  Rays may also hit balls after being
  55. ; reflected. Consider the H on the bottom next to the 4.  It bounces off
  56. ; the NW-ern most ball and hits the central ball.  A ray shot from above
  57. ; the right side 5 would hit the SE-ern most ball.  The R beneath the 5
  58. ; is because the ball is returned instantly.  It is not allowed into
  59. ; the box if it would reflect immediately.  The R on the top is a more
  60. ; leisurely return.  Both central balls would tend to deflect it east
  61. ; or west, but it cannot go either way, so it just retreats.
  62. ;
  63. ; At the end of the game, if you've placed guesses for as many balls as
  64. ; there are in the box, the true board position will be revealed.  Each
  65. ; `x' is an incorrect guess of yours; `o' is the true location of a ball.
  66.  
  67. ;;; Code:
  68.  
  69. (defvar blackbox-mode-map nil "")
  70.  
  71. (if blackbox-mode-map
  72.     ()
  73.   (setq blackbox-mode-map (make-keymap))
  74.   (suppress-keymap blackbox-mode-map t)
  75.   (define-key blackbox-mode-map "\C-f" 'bb-right)
  76.   (define-key blackbox-mode-map [right] 'bb-right)
  77.   (define-key blackbox-mode-map "\C-b" 'bb-left)
  78.   (define-key blackbox-mode-map [left] 'bb-left)
  79.   (define-key blackbox-mode-map "\C-p" 'bb-up)
  80.   (define-key blackbox-mode-map [up] 'bb-up)
  81.   (define-key blackbox-mode-map "\C-n" 'bb-down)
  82.   (define-key blackbox-mode-map [down] 'bb-down)
  83.   (define-key blackbox-mode-map "\C-e" 'bb-eol)
  84.   (define-key blackbox-mode-map "\C-a" 'bb-bol)
  85.   (define-key blackbox-mode-map " " 'bb-romp)
  86.   (define-key blackbox-mode-map [insert] 'bb-romp)
  87.   (define-key blackbox-mode-map "\C-m" 'bb-done)
  88.   (define-key blackbox-mode-map [kp_enter] 'bb-done))
  89.  
  90. ;; Blackbox mode is suitable only for specially formatted data.
  91. (put 'blackbox-mode 'mode-class 'special)
  92.  
  93. (defvar bb-board)
  94. (defvar bb-balls-placed)
  95. (defvar bb-x)
  96. (defvar bb-y)
  97. (defvar bb-score)
  98. (defvar bb-detour-count)
  99.  
  100. (defun blackbox-mode ()
  101.   "Major mode for playing blackbox.  To learn how to play blackbox,
  102. see the documentation for function `blackbox'.
  103.  
  104. The usual mnemonic keys move the cursor around the box.
  105. \\<blackbox-mode-map>\\[bb-bol] and \\[bb-eol] move to the beginning and end of line, respectively.
  106.  
  107. \\[bb-romp] -- send in a ray from point, or toggle a ball at point
  108. \\[bb-done] -- end game and get score
  109. "
  110.   (interactive)
  111.   (kill-all-local-variables)
  112.   (use-local-map blackbox-mode-map)
  113.   (setq truncate-lines t)
  114.   (setq major-mode 'blackbox-mode)
  115.   (setq mode-name "Blackbox"))
  116.  
  117. ;;;###autoload
  118. (defun blackbox (num)
  119.   "Play blackbox.  Optional prefix argument is the number of balls;
  120. the default is 4.
  121.  
  122. What is blackbox?
  123.  
  124. Blackbox is a game of hide and seek played on an 8 by 8 grid (the
  125. Blackbox).  Your opponent (Emacs, in this case) has hidden several
  126. balls (usually 4) within this box.  By shooting rays into the box and
  127. observing where they emerge it is possible to deduce the positions of
  128. the hidden balls.  The fewer rays you use to find the balls, the lower
  129. your score.
  130.  
  131. Overview of play:
  132.  
  133. \\<blackbox-mode-map>\
  134. To play blackbox, type \\[blackbox].  An optional prefix argument
  135. specifies the number of balls to be hidden in the box; the default is
  136. four.
  137.  
  138. The cursor can be moved around the box with the standard cursor
  139. movement keys.
  140.  
  141. To shoot a ray, move the cursor to the edge of the box and press SPC.
  142. The result will be determined and the playfield updated.
  143.  
  144. You may place or remove balls in the box by moving the cursor into the
  145. box and pressing \\[bb-romp].
  146.  
  147. When you think the configuration of balls you have placed is correct,
  148. press \\[bb-done].  You will be informed whether you are correct or
  149. not, and be given your score.  Your score is the number of letters and
  150. numbers around the outside of the box plus five for each incorrectly
  151. placed ball.  If you placed any balls incorrectly, they will be
  152. indicated with `x', and their actual positions indicated with `o'.
  153.  
  154. Details:
  155.  
  156. There are three possible outcomes for each ray you send into the box:
  157.  
  158.     Detour: the ray is deflected and emerges somewhere other than
  159.         where you sent it in.  On the playfield, detours are
  160.         denoted by matching pairs of numbers -- one where the
  161.         ray went in, and the other where it came out.
  162.  
  163.     Reflection: the ray is reflected and emerges in the same place
  164.         it was sent in.  On the playfield, reflections are
  165.         denoted by the letter `R'.
  166.  
  167.     Hit:    the ray strikes a ball directly and is absorbed.  It does
  168.         not emerge from the box.  On the playfield, hits are
  169.         denoted by the letter `H'.
  170.  
  171. The rules for how balls deflect rays are simple and are best shown by
  172. example.
  173.  
  174. As a ray approaches a ball it is deflected ninety degrees.  Rays can
  175. be deflected multiple times.  In the diagrams below, the dashes
  176. represent empty box locations and the letter `O' represents a ball.
  177. The entrance and exit points of each ray are marked with numbers as
  178. described under \"Detour\" above.  Note that the entrance and exit
  179. points are always interchangeable.  `*' denotes the path taken by the
  180. ray.
  181.  
  182. Note carefully the relative positions of the ball and the ninety
  183. degree deflection it causes.
  184.  
  185.     1                                            
  186.   - * - - - - - -         - - - - - - - -         - - - - - - - -       
  187.   - * - - - - - -         - - - - - - - -         - - - - - - - -       
  188. 1 * * - - - - - -         - - - - - - - -         - O - - - - O -       
  189.   - - O - - - - -         - - O - - - - -         - - * * * * - -
  190.   - - - - - - - -         - - - * * * * * 2     3 * * * - - * - -
  191.   - - - - - - - -         - - - * - - - -         - - - O - * - -      
  192.   - - - - - - - -         - - - * - - - -         - - - - * * - -       
  193.   - - - - - - - -         - - - * - - - -         - - - - * - O -       
  194.                                 2                         3
  195.  
  196. As mentioned above, a reflection occurs when a ray emerges from the same point
  197. it was sent in.  This can happen in several ways:
  198.  
  199.                                                                            
  200.   - - - - - - - -         - - - - - - - -          - - - - - - - -
  201.   - - - - O - - -         - - O - O - - -          - - - - - - - -
  202. R * * * * - - - -         - - - * - - - -          O - - - - - - -
  203.   - - - - O - - -         - - - * - - - -        R - - - - - - - -
  204.   - - - - - - - -         - - - * - - - -          - - - - - - - -
  205.   - - - - - - - -         - - - * - - - -          - - - - - - - -
  206.   - - - - - - - -       R * * * * - - - -          - - - - - - - -
  207.   - - - - - - - -         - - - - O - - -          - - - - - - - -
  208.  
  209. In the first example, the ray is deflected downwards by the upper
  210. ball, then left by the lower ball, and finally retraces its path to
  211. its point of origin.  The second example is similar.  The third
  212. example is a bit anomalous but can be rationalized by realizing the
  213. ray never gets a chance to get into the box.  Alternatively, the ray
  214. can be thought of as being deflected downwards and immediately
  215. emerging from the box.
  216.  
  217. A hit occurs when a ray runs straight into a ball:
  218.  
  219.   - - - - - - - -         - - - - - - - -          - - - - - - - -
  220.   - - - - - - - -         - - - - - - - -          - - - - O - - -
  221.   - - - - - - - -         - - - - O - - -        H * * * * - - - -
  222.   - - - - - - - -       H * * * * O - - -          - - - * - - - -
  223.   - - - - - - - -         - - - - O - - -          - - - O - - - -
  224. H * * * O - - - -         - - - - - - - -          - - - - - - - -
  225.   - - - - - - - -         - - - - - - - -          - - - - - - - -
  226.   - - - - - - - -         - - - - - - - -          - - - - - - - -
  227.  
  228. Be sure to compare the second example of a hit with the first example of
  229. a reflection."
  230.   (interactive "P")
  231.   (switch-to-buffer "*Blackbox*")
  232.   (blackbox-mode)
  233.   (setq buffer-read-only t)
  234.   (buffer-disable-undo (current-buffer))
  235.   (make-local-variable 'bb-board)
  236.   (setq bb-board (bb-init-board (or num 4)))
  237.   (make-local-variable 'bb-balls-placed)
  238.   (setq bb-balls-placed nil)
  239.   (make-local-variable 'bb-x)
  240.   (setq bb-x -1)
  241.   (make-local-variable 'bb-y)
  242.   (setq bb-y -1)
  243.   (make-local-variable 'bb-score)
  244.   (setq bb-score 0)
  245.   (make-local-variable 'bb-detour-count)
  246.   (setq bb-detour-count 0)
  247.   (bb-insert-board)
  248.   (bb-goto (cons bb-x bb-y)))
  249.  
  250. (defun bb-init-board (num-balls)
  251.   (random t)
  252.   (let (board pos)
  253.     (while (>= (setq num-balls (1- num-balls)) 0)
  254.       (while
  255.       (progn
  256.         (setq pos (cons (random 8) (random 8)))
  257.         (bb-member pos board)))
  258.       (setq board (cons pos board)))
  259.     board))
  260.  
  261. (defun bb-insert-board ()
  262.   (let (i (buffer-read-only nil))
  263.     (erase-buffer)
  264.     (insert "                     \n")
  265.     (setq i 8)
  266.     (while (>= (setq i (1- i)) 0)
  267.       (insert "   - - - - - - - -   \n"))
  268.     (insert "                     \n")
  269.     (insert (format "\nThere are %d balls in the box" (length bb-board)))
  270.     ))
  271.  
  272. (defun bb-right ()
  273.   (interactive)
  274.   (if (= bb-x 8)
  275.       ()
  276.     (forward-char 2)
  277.     (setq bb-x (1+ bb-x))))
  278.  
  279. (defun bb-left ()
  280.   (interactive)
  281.   (if (= bb-x -1)
  282.       ()
  283.     (backward-char 2)
  284.     (setq bb-x (1- bb-x))))
  285.  
  286. (defun bb-up ()
  287.   (interactive)
  288.   (if (= bb-y -1)
  289.       ()
  290.     (previous-line 1)
  291.     (setq bb-y (1- bb-y))))
  292.  
  293. (defun bb-down ()
  294.   (interactive)
  295.   (if (= bb-y 8)
  296.       ()
  297.     (next-line 1)
  298.     (setq bb-y (1+ bb-y))))
  299.  
  300. (defun bb-eol ()
  301.   (interactive)
  302.   (setq bb-x 8)
  303.   (bb-goto (cons bb-x bb-y)))
  304.  
  305. (defun bb-bol ()
  306.   (interactive)
  307.   (setq bb-x -1)
  308.   (bb-goto (cons bb-x bb-y)))
  309.  
  310. (defun bb-romp ()
  311.   (interactive)
  312.   (cond
  313.    ((and
  314.      (or (= bb-x -1) (= bb-x 8))
  315.      (or (= bb-y -1) (= bb-y 8))))
  316.    ((bb-outside-box bb-x bb-y)
  317.     (bb-trace-ray bb-x bb-y))
  318.    (t
  319.     (bb-place-ball bb-x bb-y))))
  320.  
  321. (defun bb-place-ball (x y)
  322.   (let ((coord (cons x y)))
  323.     (cond
  324.      ((bb-member coord bb-balls-placed)
  325.       (setq bb-balls-placed (bb-delete coord bb-balls-placed))
  326.       (bb-update-board "-"))
  327.      (t
  328.       (setq bb-balls-placed (cons coord bb-balls-placed))
  329.       (bb-update-board "O")))))
  330.  
  331. (defun bb-trace-ray (x y)
  332.   (let ((result (bb-trace-ray-2
  333.          t
  334.          x
  335.          (cond
  336.           ((= x -1) 1)
  337.           ((= x 8) -1)
  338.           (t 0))
  339.          y
  340.          (cond
  341.           ((= y -1) 1)
  342.           ((= y 8) -1)
  343.           (t 0)))))
  344.     (cond
  345.      ((eq result 'hit)
  346.       (bb-update-board "H")
  347.       (setq bb-score (1+ bb-score)))
  348.      ((equal result (cons x y))
  349.       (bb-update-board "R")
  350.       (setq bb-score (1+ bb-score)))
  351.      (t
  352.       (setq bb-detour-count (1+ bb-detour-count))
  353.       (bb-update-board (format "%d" bb-detour-count))
  354.       (save-excursion
  355.     (bb-goto result)
  356.     (bb-update-board (format "%d" bb-detour-count)))
  357.       (setq bb-score (+ bb-score 2))))))
  358.  
  359. (defun bb-trace-ray-2 (first x dx y dy)
  360.   (cond
  361.    ((and (not first)
  362.      (bb-outside-box x y))
  363.     (cons x y))
  364.    ((bb-member (cons (+ x dx) (+ y dy)) bb-board)
  365.     'hit)
  366.    ((bb-member (cons (+ x dx dy) (+ y dy dx)) bb-board)
  367.     (bb-trace-ray-2 nil x (- dy) y (- dx)))
  368.    ((bb-member (cons (+ x dx (- dy)) (+ y dy (- dx))) bb-board)
  369.     (bb-trace-ray-2 nil x dy y dx))
  370.    (t
  371.     (bb-trace-ray-2 nil (+ x dx) dx (+ y dy) dy))))
  372.  
  373. (defun bb-done ()
  374.   "Finish the game and report score."
  375.   (interactive)
  376.   (let (bogus-balls)
  377.     (cond
  378.      ((not (= (length bb-balls-placed) (length bb-board)))
  379.       (message "There %s %d hidden ball%s; you have placed %d."
  380.            (if (= (length bb-board) 1) "is" "are")
  381.            (length bb-board)
  382.            (if (= (length bb-board) 1) "" "s")
  383.            (length bb-balls-placed)))
  384.      (t
  385.       (setq bogus-balls (bb-show-bogus-balls bb-balls-placed bb-board))
  386.       (if (= bogus-balls 0)
  387.       (message "Right!  Your score is %d." bb-score)
  388.     (message "Oops!  You missed %d ball%s.  Your score is %d."
  389.          bogus-balls
  390.          (if (= bogus-balls 1) "" "s")
  391.          (+ bb-score (* 5 bogus-balls))))
  392.       (bb-goto '(-1 . -1))))))
  393.  
  394. (defun bb-show-bogus-balls (balls-placed board)
  395.   (bb-show-bogus-balls-2 balls-placed board "x")
  396.   (bb-show-bogus-balls-2 board balls-placed "o"))
  397.  
  398. (defun bb-show-bogus-balls-2 (list-1 list-2 c)
  399.   (cond
  400.    ((null list-1)
  401.     0)
  402.    ((bb-member (car list-1) list-2)
  403.     (bb-show-bogus-balls-2 (cdr list-1) list-2 c))
  404.    (t
  405.     (bb-goto (car list-1))
  406.     (bb-update-board c)
  407.     (1+ (bb-show-bogus-balls-2 (cdr list-1) list-2 c)))))
  408.  
  409. (defun bb-outside-box (x y)
  410.   (or (= x -1) (= x 8) (= y -1) (= y 8)))
  411.  
  412. (defun bb-goto (pos)
  413.   (goto-char (+ (* (car pos) 2) (* (cdr pos) 22) 26)))
  414.  
  415. (defun bb-update-board (c)
  416.   (let ((buffer-read-only nil))
  417.     (backward-char (1- (length c)))
  418.     (delete-char (length c))
  419.     (insert c)
  420.     (backward-char 1)))
  421.   
  422. (defun bb-member (elt list)
  423.   "Returns non-nil if ELT is an element of LIST."
  424.   (eval (cons 'or (mapcar (function (lambda (x) (equal x elt))) list))))
  425.  
  426. (defun bb-delete (item list)
  427.   "Deletes ITEM from LIST and returns a copy."
  428.   (cond
  429.    ((equal item (car list)) (cdr list))
  430.    (t (cons (car list) (bb-delete item (cdr list))))))
  431.  
  432. ;;; blackbox.el ends here
  433.  
  434.  
  435.